package fule.com.appbarlayoutdemo.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;

import java.util.ArrayList;
import java.util.List;

import fule.com.appbarlayoutdemo.Fragment.AllFragment;
import fule.com.appbarlayoutdemo.Fragment.BeautyFragment;
import fule.com.appbarlayoutdemo.Fragment.MusicFragment;
import fule.com.appbarlayoutdemo.Fragment.OnLineFragment;
import fule.com.appbarlayoutdemo.R;
import fule.com.appbarlayoutdemo.adapter.MyFragmentPagerAdapter;
import fule.com.appbarlayoutdemo.base.BaseActivity;
import fule.com.appbarlayoutdemo.util.GlideImageLoader;

/**
 * 作者： njb
 * 时间： 2018/12/24 13:40
 * 描述：
 * 来源：
 */
public class LiveActivity  extends BaseActivity implements View.OnClickListener{
    private Banner banner;
    private List<String> list ;
    private List<String> titles;
    private RadioGroup radioGroup;
    private RadioButton rb_online,rb_all,rb_beauty,rb_music;
    private ViewPager viewPager;
    private FragmentManager fm= getSupportFragmentManager();
    private List<Fragment> mList ;
    private AllFragment allFragment;
    private OnLineFragment onLineFragment;
    private BeautyFragment beautyFragment;
    private MusicFragment musicFragment;
    private MyFragmentPagerAdapter pagerAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live);
        initView();
        initBanner();
        initFragment();
        setListener();
    }

    private void initFragment() {
        mList = new ArrayList<>();
        onLineFragment = new OnLineFragment();
        allFragment = new AllFragment();
        beautyFragment = new BeautyFragment();
        musicFragment = new MusicFragment();

        mList.add(onLineFragment);
        mList.add(allFragment);
        mList.add(beautyFragment);
        mList.add(musicFragment);

        pagerAdapter = new MyFragmentPagerAdapter(fm,mList);
        viewPager.setAdapter(pagerAdapter);
        viewPager.setCurrentItem(0);
        viewPager.setOffscreenPageLimit(mList.size());


        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position){
                    case 0:
                        rb_online.setChecked(true);
                        break;
                    case 1:
                        rb_all.setChecked(true);
                        break;
                    case 2:
                        rb_beauty.setChecked(true);
                        break;
                    case 3:
                        rb_music.setChecked(true);
                        break;
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void setListener() {
       rb_online.setOnClickListener(this);
        rb_all.setOnClickListener(this);
        rb_beauty.setOnClickListener(this);
        rb_music.setOnClickListener(this);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.rb_online:
                     viewPager.setCurrentItem(0);
                        break;
                    case R.id.rb_all:
                        viewPager.setCurrentItem(1);
                        break;
                    case R.id.rb_beauty:
                        viewPager.setCurrentItem(2);
                        break;
                    case R.id.rb_music:
                        viewPager.setCurrentItem(3);
                        break;
                }
            }
        });

    }

    private void initView() {
        banner = findViewById(R.id.live_bannner);
        rb_online = findViewById(R.id.rb_online);
        rb_all = findViewById(R.id.rb_all);
        rb_beauty = findViewById(R.id.rb_beauty);
        rb_music = findViewById(R.id.rb_music);
        viewPager = findViewById(R.id.viewPager);
        radioGroup = findViewById(R.id.radioGroup);

    }

    private void initBanner() {
        list = new ArrayList<>();
        titles = new ArrayList<>();
        list.add("http://a3.att.hudong.com/72/76/01300000012339118647690465772.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic259ohaj30ci08c74r.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic21363tj30ci08ct96.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2b16zuj30ci08cwf4.jpg");
        list.add("http://ww4.sinaimg.cn/large/006uZZy8jw1faic2e7vsaj30ci08cglz.jpg");

        titles.add("推荐");
        titles.add("全部");
        titles.add("美女");
        titles.add("搞笑");
        titles.add("音乐");

        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR);//设置圆形指示器与标题
        banner.setIndicatorGravity(BannerConfig.RIGHT);//设置指示器位置
        banner.setDelayTime(2000);//设置轮播时间
        banner.setImages(list).setImageLoader(new GlideImageLoader());//设置图片源
         banner.setBannerTitles(titles);//设置标题源
        banner.start();

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
           /* case R.id.rb_online:
                rb_online.setChecked(true);
                rb_all.setChecked(false);
                rb_beauty.setChecked(false);
                rb_music.setChecked(false);
                break;
            case R.id.rb_all:
                rb_online.setChecked(false);
                rb_all.setChecked(true);
                rb_beauty.setChecked(false);
                rb_music.setChecked(false);
                break;
            case R.id.rb_beauty:
                rb_online.setChecked(false);
                rb_all.setChecked(false);
                rb_beauty.setChecked(true);
                rb_music.setChecked(false);
                break;
            case R.id.rb_music:
                rb_online.setChecked(false);
                rb_all.setChecked(false);
                rb_beauty.setChecked(false);
                rb_music.setChecked(true);
                break;*/
        }
    }
}
